package cate.game.role.res.guild.po.my.mall;

import cate.game.role.Role;
import cate.game.role.mall.Commodity;
import cate.game.role.mall.RefreshTimeCheck;
import cate.common.table.guild.GuildMallItemRow;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteField;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.annotation.Transient;

public class GuildMallItem extends Commodity {
    @NoteField(value = "上次刷新时间")
    public long lastRefreshTime;

    @Transient
    @JsonIgnore
    @Getter
    protected transient Role role;

    @JsonIgnore
    public GuildMallItemRow getRow() {
        return role.getGame().table.guild.mallItem.get(tid);
    }

    public GuildMallItem(Role role, int tid) {
        super(tid);
        this.lastRefreshTime = System.currentTimeMillis();
        this.role = role;
    }

    public GuildMallItem() {}

    public void initialize(Role role) {
        this.role = role;
        checkRefresh();
    }

    public void onDaySpan() {
        checkRefresh();
    }

    private void checkRefresh() {
        GuildMallItemRow row = getRow();
        if (row == null) {
            return;
        }
        if (StringUtils.isBlank(row.autoCycle)) {
            return;
        }
        if (RefreshTimeCheck.isRefresh(role.getGame(), lastRefreshTime, row.autoCycle)) {
            purchaseNum = 0;
            lastRefreshTime = System.currentTimeMillis();
        }
    }
}
